﻿using System.Collections.ObjectModel;
using DesignPattern.Models;
using DesignPattern.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using Wpf.Ui.Controls;

namespace DesignPattern.ViewModels.Windows
{
    public partial class MainWindowViewModel : ObservableObject
    {
        private readonly DesignPatternService _designPatternService;
        [ObservableProperty] private string _applicationTitle = "C# 设计模式示例";
        [ObservableProperty] private ObservableCollection<object> _menuItems;

        public MainWindowViewModel(DesignPatternService designPatternService)
        {
            _designPatternService = designPatternService;
            _menuItems = CreateMenuItems();
        }

        private ObservableCollection<object> CreateMenuItems()
        {
            var items = new ObservableCollection<object>
            {
                new NavigationViewItem()
                {
                    Content = "首页",
                    Icon = new SymbolIcon { Symbol = SymbolRegular.HomeMore24 },
                    TargetPageType = typeof(Views.Pages.DashboardPage),
                }
            };
            //【1】创建型模式菜单
            var creationalPatternsItem = new NavigationViewItem()
            {
                Content = "创建型模式",
                Icon = new SymbolIcon { Symbol = SymbolRegular.AppsList20 },
                TargetPageType = typeof(Views.Pages.CreationalPage),
                // MenuItemsSource = new object[]
                // {
                //     new NavigationViewItem(nameof(Anchor), typeof(AnchorPage)),
                // }
            };
            foreach (var pattern in _designPatternService.GetPatternsByType(PatternType.Creational))
            {
                creationalPatternsItem.MenuItems.Add(new NavigationViewItem()
                {
                    Content = pattern.Name,
                    Tag = pattern.Name,
                    TargetPageTag = pattern.Name,
                    TargetPageType = typeof(Views.Pages.DesignPatternPage)
                });
            }

            //【2】结构型模式菜单
            var structuralPatternsItem = new NavigationViewItem()
            {
                Content = "结构型模式",
                Icon = new SymbolIcon { Symbol = SymbolRegular.AppFolder24 },
                TargetPageType = typeof(Views.Pages.StructuralPage),
            };

            foreach (var pattern in _designPatternService.GetPatternsByType(PatternType.Structural))
            {
                structuralPatternsItem.MenuItems.Add(new NavigationViewItem()
                {
                    Content = pattern.Name,
                    Tag = pattern.Name,
                    TargetPageTag = pattern.Name,
                    TargetPageType = typeof(Views.Pages.DesignPatternPage)
                });
            }

            //【3】行为型模式菜单
            var behavioralPatternsItem = new NavigationViewItem()
            {
                Content = "行为型模式",
                Icon = new SymbolIcon { Symbol = SymbolRegular.AppGeneric32 },
                TargetPageType = typeof(Views.Pages.BehavioralPage),
            };

            foreach (var pattern in _designPatternService.GetPatternsByType(PatternType.Behavioral))
            {
                behavioralPatternsItem.MenuItems.Add(new NavigationViewItem()
                {
                    Content = pattern.Name,
                    Tag = pattern.Name,
                    TargetPageTag = pattern.Name,
                    TargetPageType = typeof(Views.Pages.DesignPatternPage)
                });
            }

            items.Add(creationalPatternsItem);
            items.Add(structuralPatternsItem);
            items.Add(behavioralPatternsItem);
            return items;
        }

        [ObservableProperty] private ObservableCollection<object> _footerMenuItems = new()
        {
            new NavigationViewItem()
            {
                Content = "设置",
                Icon = new SymbolIcon { Symbol = SymbolRegular.SlideSettings24 },
                TargetPageType = typeof(Views.Pages.SettingsPage)
            }
        };
    }
}